﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExcelUtilities.Impl
{
    /// <summary>
    /// write excel by epplus
    /// </summary>
    public class EPPlusExcelWriter : ExcelWriter
    {
        OfficeOpenXml.ExcelPackage excel;

        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="tempFile">模板文件</param>
        protected override void Open(string tempFile)
        {
            if (string.IsNullOrEmpty(tempFile))
            {
                excel = new OfficeOpenXml.ExcelPackage();
            }
            else
            {
                excel = new OfficeOpenXml.ExcelPackage(new System.IO.FileInfo(tempFile));
            }
        }
        /// <summary>
        /// 关闭，导出完成后调用
        /// </summary>
        protected override void Close()
        {
            excel.Dispose();
        }
        /// <summary>
        /// 导出到excel文件
        /// </summary>
        /// <param name="savefile">将导出到的excek文件</param>
        protected override void Save(string savefile)
        {
            excel.SaveAs(new System.IO.FileInfo(savefile));
        }
        /// <summary>
        /// 导出到Stream
        /// </summary>
        /// <param name="stream"></param>
        protected override void Save(System.IO.Stream stream)
        {
            excel.SaveAs(stream);
        }
        /// <summary>
        /// 给单元格赋值
        /// </summary>
        /// <param name="sheetOrder">第几个工作表（从1开始）</param>
        /// <param name="cellAddress">单元格地址</param> 
        /// <param name="value">将赋给单元格的值</param>
        protected override void SetCellValue(int sheetOrder, string cellAddress, object value)
        {
            excel.Workbook.Worksheets[sheetOrder].Cells[cellAddress].Value = value;
        }
        /// <summary>
        /// 给单元格赋值
        /// </summary>
        /// <param name="sheetOrder">第几个工作表（从1开始）</param>
        /// <param name="row">单元格在第几行(从1开始)</param>
        /// <param name="col">单元格在第几列(从1开始)</param>
        /// <param name="value">将赋给单元格的值</param>
        protected override void SetCellValue(int sheetOrder, int row, int col, object value)
        {
            excel.Workbook.Worksheets[sheetOrder].Cells[row, col].Value = value;
        }
        /// <summary>
        /// 给单元格赋值
        /// </summary>
        /// <param name="sheetName">工作表名</param>
        /// <param name="cellAddress">单元格地址</param>
        /// <param name="value">将赋给单元格的值</param>
        protected override void SetCellValue(string sheetName, string cellAddress, object value)
        {
            GetOrNew(sheetName).Cells[cellAddress].Value = value;
        }
        /// <summary>
        /// 给单元格赋值
        /// </summary>
        /// <param name="sheetName">工作表名</param>
        /// <param name="row">单元格在第几行(从1开始)</param>
        /// <param name="col">单元格在第几列(从1开始)</param>
        /// <param name="value">将赋给单元格的值</param>
        protected override void SetCellValue(string sheetName, int row, int col, object value)
        {
            GetOrNew(sheetName).Cells[row, col].Value = value;
        }

        private OfficeOpenXml.ExcelWorksheet GetOrNew(string sheetName)
        {
            var worksheet = excel.Workbook.Worksheets[sheetName];
            if (worksheet == null)
            {
                worksheet = excel.Workbook.Worksheets.Add(sheetName);
            }
            return worksheet;
        }
        private OfficeOpenXml.ExcelWorksheet GetOrNew(int sheetOrder)
        {
            var worksheet = excel.Workbook.Worksheets[sheetOrder];
            if (worksheet == null)
            {
                worksheet = excel.Workbook.Worksheets.Add("sheet" + sheetOrder.ToString());
            }
            return worksheet;
        }
    }
}